/**
 * Created by moyu on 2017/4/9.
 */
const cheerio = require('cheerio');

Object.defineProperties(cheerio.prototype, {
    'outerHTML': {
        get: function () {
            return this.clone().wrap('<container />').parent().html();
        }
    },
    'innerText': {
        get: function () {
            var html = this.html();
            if(!html) {
                // return he.decode(this.text());
                return this.text();
            }
            var myhtml = html.replace(/<p.*?>(.*?)<\/p>/gmi, '$1\n')
                .replace(/<div.*?>(.*?)<\/div>/gmi, '$1\n')
                .replace(/<br.*?>/gmi, '\n')
                .replace(/<(?:.)*?>/gm, '') // remove all html tags
            // var mytext = he.decode(myhtml)
            return myhtml;
        }
    },
});

cheerio.myLoad = function (html, options) {
    html = `<cheerio id="cheerio">${html}</cheerio>`
    const $ = this.load(html, Object.assign({decodeEntities: true}, options));
    return $('cheerio#cheerio');
}


const express = require('express');

const normalize = (code, result) => ({code, result});
express.response.normalize = function (code, result) {
    this.json(normalize(code, result))
};